package com.hwg.emp.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.dao.EmployeeDAO;
import com.hwg.emp.dto.EmpDTO;
import com.hwg.emp.model.DeptMD;
import com.hwg.emp.model.EmployeeMD;
import com.hwg.emp.model.PageBean;

public class EmployeeDAOImpl implements EmployeeDAO{
	@Override
	public EmpDTO selectOwnInfoById(String id) throws Exception {
		EmpDTO empDto=new EmpDTO();
		String sql="select * from emp_employee e,sys_user u where e.id=? and u.id=e.id";
		Connection con=DBUtil.getConnection();
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setString(1, id);
		ResultSet rs=ps.executeQuery();
		if(rs.next()){
			empDto.setId(rs.getString("e.id"));
			empDto.setAccount(rs.getString("account"));
			empDto.setName(rs.getString("name"));
			Integer sex=rs.getInt("sex");
			String ssex=null;
			DeptMD deptMd=new DeptMD();
			//deptMd.setName("人事部");
			deptMd.setId(rs.getString("dept_id"));
			if(sex==0){
				ssex="女"; 
			}else{
				ssex="男";
			}
			empDto.setSex(ssex);
			empDto.setAge(rs.getInt("age"));
			empDto.setBirth(rs.getDate("birth"));
			empDto.setPhone(rs.getString("phone"));
			empDto.setDept(deptMd);
		}
		return empDto;
	}
	
	@Override
	public int update(EmployeeMD t) throws Exception {
		return 0;
		
	}
	@Override
	public EmployeeMD select(String uk, String ukValue) throws Exception {
		return null;
	}
	@Override
	public boolean exist(String uk, String ukValue) throws Exception {
		return false;
	}
	
	/**
	 * 增加员工
	 * @return 
	 */
	@Override
	public int insert(EmployeeMD t) throws Exception {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO emp_employee (id,sex,age,birth,phone,dept_id) VALUES (?,?,?,?,?,?)";
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, t.getId());
		ps.setString(2,t.getSex());
		ps.setString(3, t.getAge());
		ps.setDate(4, (Date) t.getBirth());
		ps.setString(5, t.getPhone());
		ps.setString(6, t.getDept().getId());
		int i =ps.executeUpdate();
		return i ;
	}

	@Override
	public List<EmployeeMD> selectEmployee(String account,String name,String age,String phone, PageBean pagebean) throws Exception {
		// TODO Auto-generated method stub
		String sql = "SELECT sys_user.account,sys_user.`name`,emp_employee.age,emp_employee.birth,emp_employee.phone,emp_employee.sex FROM emp_employee,sys_user where (sys_user.account like'%?%') OR (sys_user.`name` LIKE '%?%') OR (emp_employee.age LIKE '%?%') OR (emp_employee.phone LIKE '%?%') GROUP BY account";
		Connection con =DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		
		return null;
	}

	@Override
	public int delete(String uk, String ukValue) throws Exception {
		return 0;
	}

	@Override
	public String selectEmployeeDeptId(String userId) throws Exception {
		// TODO Auto-generated method stub
		String deptId=null;
		String sql="SELECT emp_employee.dept_id from sys_user,emp_employee WHERE sys_user.id=emp_employee.id AND sys_user.id=?";
		Connection con=DBUtil.getConnection();
		PreparedStatement pst=con.prepareStatement(sql);
		pst.setString(1, userId);
		ResultSet rs=pst.executeQuery();
		if(rs.next()){
			deptId=rs.getString("dept_id");
		}
		return deptId;
	}
     
}
